package cn.edu.hqu.cst.crj.serversocket2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.input) EditText main_content;
    Button send;
    TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);//注意位置 否则报错
        send=findViewById(R.id.send);


        new Thread(new ClientThread()).start();
        //Toast.makeText(this,"ces",Toast.LENGTH_SHORT).show();
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(this,"ces",Toast.LENGTH_SHORT).show();
                Message msg=new Message();
                msg.what=0x1234;
                Bundle bundle=new Bundle();
                bundle.putString("sendContent",main_content.getText().toString());
                msg.setData(bundle);
                ClientThread.sendHandler.sendMessage(msg);
            }
        });


    }
/*
    @OnClick(R.id.btn_send)
    void send(){
       Toast.makeText(this,"ces",Toast.LENGTH_SHORT).show();
        Message msg=new Message();
        msg.what=0x1234;
        Bundle bundle=new Bundle();
        bundle.putString("sendContent",main_content.getText().toString());
        msg.setData(bundle);
        ClientThread.sendHandler.sendMessage(msg);
    }
*/
}


class ClientThread implements Runnable{
    DatagramSocket remoteDs=null;
    InetAddress remoteAddr=null;
    byte[]buf=new byte[1024];
    private  final static int remotePort=5000;

    public static Handler sendHandler;
    public  ClientThread(){
        try{
            remoteDs= new DatagramSocket();
            remoteAddr=InetAddress.getByName("127.0.0.1");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    @Override
    public void run(){
        new Thread(){
            public  void run(){
                Looper.prepare();
                // 客户端启动ClientThread线程不断读取来自服务器的数据
                sendHandler=new Handler(){
                    @Override

                    public void handleMessage(Message msg){
                        // 如果消息来自于子线程
                        if(msg.what==0x1234) {
                            // 将读取的内容追加显示在文本框中
                            byte[] buffer = msg.getData().getString("sendContent").getBytes();
                            DatagramPacket remoteDp = new DatagramPacket(buffer, buffer.length, remoteAddr, remotePort);
                            try {
                                remoteDs.send(remoteDp);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
                Looper.loop();
            }
        }.start();

    }

}

